package com.fqgj.xjd.user.service.impl;

import com.fqgj.common.api.Response;
import com.fqgj.common.utils.StringUtils;
import com.fqgj.exception.common.ApiErrorException;
import com.fqgj.xjd.user.client.UserIntegralService;
import com.fqgj.xjd.user.client.request.UserIntegralAddRequest;
import com.fqgj.xjd.user.client.response.UserIntegralInfo;
import com.fqgj.xjd.user.dao.UserBaseIntegralDao;
import com.fqgj.xjd.user.entity.UserBaseIntegralEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 赵洋～
 * @Description:
 * @Date: Create in 15:07 2019-06-21
 */
@Service("userIntegralService")
public class UserIntegralServiceImpl implements UserIntegralService {

    @Autowired
    private UserBaseIntegralDao userBaseIntegralDao;

    @Override
    public Response<UserIntegralInfo> getUserIntegralInfoByUserCode(String userCode) {

        if (StringUtils.isEmpty(userCode)) {
            throw new ApiErrorException("userCode is empty");
        }
        Map<String, Object> map = new HashMap<>(1);
        map.put("userCode", userCode);
        List<UserBaseIntegralEntity> userBaseIntegralEntityList = userBaseIntegralDao.selectByParams(map);
        if (userBaseIntegralEntityList == null || userBaseIntegralEntityList.isEmpty()) {
            return Response.ok().putData(null);
        }

        UserIntegralInfo userIntegralInfo = new UserIntegralInfo()
                .setUserCode(userBaseIntegralEntityList.get(0).getUserCode())
                .setStartNum(userBaseIntegralEntityList.get(0).getIntegralNumber());

        return Response.ok(userIntegralInfo);
    }

    @Override
    public Response<Boolean> addOrUpdateIntegral(UserIntegralAddRequest userIntegralAddRequest) {
        if (StringUtils.isEmpty(userIntegralAddRequest.getUserCode())) {
            throw new ApiErrorException("userCode is empty");
        }
        if (userIntegralAddRequest.getAddStarNum() == null && userIntegralAddRequest.getAddStarNum() == 0) {
            throw new ApiErrorException("add startNum is empty");
        }

        UserBaseIntegralEntity userBaseIntegralEntity = userBaseIntegralDao.selectByUserCode(userIntegralAddRequest.getUserCode());

        if (userBaseIntegralEntity == null) {
            UserBaseIntegralEntity insertEntity = new UserBaseIntegralEntity();
            insertEntity.setUserCode(userIntegralAddRequest.getUserCode());
            insertEntity.setIntegralNumber(userIntegralAddRequest.getAddStarNum());
            userBaseIntegralDao.insert(insertEntity);
        } else {
            Integer startNum = userBaseIntegralEntity.getIntegralNumber() + userIntegralAddRequest.getAddStarNum();
            userBaseIntegralEntity.setIntegralNumber(startNum);
            userBaseIntegralDao.updateByPrimaryKey(userBaseIntegralEntity);
        }

        return Response.ok().putData(Boolean.TRUE);
    }
}
